package com.xz.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xz.reggie.common.R;
import com.xz.reggie.entity.Orders;
import com.xz.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 许正
 * @version 1.0
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据: {}", orders);

        ordersService.submit(orders);

        return R.success("下单成功");
    }

    @GetMapping("/userPage")
    public R<Page> userPage(int page, int pageSize) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByAsc(Orders::getOrderTime);
        ordersService.page(ordersPage, ordersLambdaQueryWrapper);
        return R.success(ordersPage);
    }

    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String number) {
        log.info("page: {}, pageSize: {}, number: {}", page, pageSize, number);

        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Orders> ordersLambdaQueryWrapper = new LambdaQueryWrapper<>();
        ordersLambdaQueryWrapper.orderByAsc(Orders::getOrderTime);
        ordersLambdaQueryWrapper.eq(StringUtils.isNotEmpty(number), Orders::getNumber, number);

        ordersService.page(pageInfo, ordersLambdaQueryWrapper);
        return R.success(pageInfo);
    }
}
